//
//  MXFaceRecgnitionVC.m
//  MXLoginMethodsDomo
//
//  Created by 马彦 on 16/10/11.
//  Copyright © 2016年 MX. All rights reserved.
//

#import "MXFaceRecgnitionVC.h"
#import "UITableViewCell+Cellutils.h"
#import "MXFaceRecgnitionCell.h"

/**人脸识别登录*/
#import "SuperID.h"
#import "MXFaceRecgnitionModel.h"

@interface MXFaceRecgnitionVC () <UITableViewDelegate,UITableViewDataSource,SuperIDDelegate,MXFaceRecgnitionCellDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) SuperID *faceSuper;

@end

@implementation MXFaceRecgnitionVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [SuperID sharedInstance].delegate = self;
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *FaceRecgnitionCell = @"FaceRecgnitionCell";
    
    MXFaceRecgnitionCell *cell = [tableView dequeueReusableCellWithIdentifier:FaceRecgnitionCell];
    if (!cell) {
        cell = [[MXFaceRecgnitionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FaceRecgnitionCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.delegate = self;
    cell.titleStr = @"脸部识别登录";
    [cell addBottomHorizontalLine:5];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return CellHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}


- (void)faceRecgnitionWithSwitch:(UISwitch *)swit {
    if (swit.on) {
        //打开脸部识别登录
//         [self faceLogin];
    }else {
    //关闭脸部识别登录
//        [[SuperID sharedInstance] userCancelAuthorization];
    }
}

/*脸部识别*/
- (void)faceLogin {
    NSError *error = nil;
    
    id SIDLoginViewController = [[SuperID sharedInstance]obtainLoginViewControllerWithError:&error];
    if (SIDLoginViewController) {
        
        [self presentViewController:SIDLoginViewController animated:YES completion:nil];
        
    }else{
        
        NSLog(@"Error =%ld,%@",(long)[error code],[error localizedDescription]);
    }
}

#pragma mark - 

//应用用户使用SuperID登录应用操作完成调用的协议方法，开发者可通过继承该协议方法获取用户信息。
- (void)superID:(SuperID *)sender userDidFinishLoginWithUserInfo:(NSDictionary *)userInfo withOpenId:(NSString *)openId error:(NSError *)error{
    
    if (!error) {
        [MXFaceRecgnitionModel setNewPassword:userInfo[@"phone"]];
        
        //授权登录成功
        NSLog(@"userInfo:%@", userInfo);
        NSLog(@"openId = %@", openId);
        
    }else{
        
        //授权登录失败
        NSLog(@"Login Fail Error =%ld,%@",(long)[error code],[error localizedDescription]);
    }
}

/**
 *  应用用户取消当前应用账号与一登账号的授权关联成功协议方法
 */
- (void)superID:(SuperID *)sender userDidFinishCancelAuthorization:(NSError *)error {
    
}

/**
 *  用户在授权状态下使用一登人脸信息获取成功的协议方法
 */
- (void)superID:(SuperID *)sender userDidFinishGetFaceFeatureWithFeatureInfo:(NSDictionary *) featureInfo error:(NSError *)error {
    
}

/**
 *  查询用户与一登账号授权关联状态的协议方法
 */
- (void)superID:(SuperID *)sender queryCurrentUserAuthorizationStateResponse:(SIDUserAuthorizationState)state {
    
}

/**
 *  用户验证结果回调
 */
- (void)superID:(SuperID *)sender faceVerifyResponse:(SIDFACEVerifyState)state {
    
}


#pragma mark -lazy

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = RGBA_Color(0.97, 0.97, 0.97, 1.0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

@end
